const axios = require('axios');
const fs = require('fs');
const FormData = require('form-data');

const appID = 'wxecc9322469a3f41b';
const appSecret = '7803a7c5261cd59ce2fcc64b7d212b4f';

async function getAccessToken() {
    const response = await axios.get(`https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${appID}&secret=${appSecret}`);
    console.log('response.data', response.data)
    return response.data.access_token;
 
}

async function uploadImage(accessToken) {
    const imageUrl = '../头像.jpg'; // 替换为你的图片路径
    const form = new FormData();
    form.append('media', fs.createReadStream(imageUrl));

    const response = await axios.post(`https://api.weixin.qq.com/cgi-bin/media/upload?access_token=${accessToken}&type=image`, form, {
      headers: form.getHeaders()
    });

    return response.data.media_id;
  
}

async function getThumbMediaID() {
    console.log('2')
    const accessToken = await getAccessToken();
    console.log('accessToken', accessToken)
    if (!accessToken) {
      return;
    }

    const mediaID = await uploadImage(accessToken);
    console.log('mediaID', mediaID)
    if (!mediaID) {
      return;
    }

    const response = await axios.post(`https://api.weixin.qq.com/cgi-bin/material/get_material?access_token=${accessToken}`, {
      media_id: mediaID
    });

    const thumbMediaID = response.data.thumb_media_id;
    console.log('Thumb Media ID:', thumbMediaID);

}

getThumbMediaID();
console.log('1')